package br.com.gerenteweb.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import br.com.gerenteweb.beans.Cliente;
import br.com.gerenteweb.daos.ClienteDao;
import br.com.gerenteweb.interfaces.ManageBeansInterface;

/**
 * ManagedBean de cliente
 * 
 * @author Felipe-
 *
 */
@SuppressWarnings("serial")
@ManagedBean(name = "clienteBean")
@SessionScoped
public class ClienteBean implements Serializable, ManageBeansInterface {

	private Cliente cliente = new Cliente();
	private ClienteDao clienteDao = new ClienteDao();
	private List<Cliente> listClientes = new ArrayList<Cliente>();
	private String nomeDaLoja;

	public ClienteBean() {

	}

	@Override
	public String adicionar() {
		try {
			clienteDao.addCliente(cliente);
			this.cliente = new Cliente();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nomeDaLoja;

	}

	@Override
	public String remover() {
		try {
			Map<String, String> params = FacesContext.getCurrentInstance()
					.getExternalContext().getRequestParameterMap();
			String idCliente = params.get("idCliente");
			Cliente cCliente = clienteDao.getPorID(Long.valueOf(idCliente));
			clienteDao.removeCliente(cCliente);
			return "lista-de-clientes";
		} catch (Exception e) {
			e.printStackTrace();
			return "lista-de-clientes";
		}
	}

	@Override
	public String carregar() {
		try {
			Map<String, String> params = FacesContext.getCurrentInstance()
					.getExternalContext().getRequestParameterMap();
			String idCliente = params.get("idCliente");
			this.cliente = clienteDao.getPorID(Long.valueOf(idCliente));
			System.out.println(this.cliente);
			return "carregar-cliente";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "lista-de-clientes";
	}

	@Override
	public void atualizar() {
		try {
			clienteDao.updateCliente(cliente);
			this.cliente = new Cliente();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void listar() {
		try {
			listClientes = clienteDao.getClientes();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void limpar() {

	}

	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	public ClienteDao getClienteDao() {
		return clienteDao;
	}

	public void setClienteDao(ClienteDao clienteDao) {
		this.clienteDao = clienteDao;
	}

	public List<Cliente> getListClientes() {
		listar();
		return listClientes;
	}

	public void setListClientes(List<Cliente> listClientes) {
		this.listClientes = listClientes;
	}

	public String getNomeDaLoja() {
		return nomeDaLoja;
	}

	public void setNomeDaLoja(String nomeDaLoja) {
		this.nomeDaLoja = nomeDaLoja;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cliente == null) ? 0 : cliente.hashCode());
		result = prime * result
				+ ((clienteDao == null) ? 0 : clienteDao.hashCode());
		result = prime * result
				+ ((listClientes == null) ? 0 : listClientes.hashCode());
		result = prime * result
				+ ((nomeDaLoja == null) ? 0 : nomeDaLoja.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ClienteBean other = (ClienteBean) obj;
		if (cliente == null) {
			if (other.cliente != null)
				return false;
		} else if (!cliente.equals(other.cliente))
			return false;
		if (clienteDao == null) {
			if (other.clienteDao != null)
				return false;
		} else if (!clienteDao.equals(other.clienteDao))
			return false;
		if (listClientes == null) {
			if (other.listClientes != null)
				return false;
		} else if (!listClientes.equals(other.listClientes))
			return false;
		if (nomeDaLoja == null) {
			if (other.nomeDaLoja != null)
				return false;
		} else if (!nomeDaLoja.equals(other.nomeDaLoja))
			return false;
		return true;
	}

}
